﻿// -----------------------------------------------------------------------
// <copyright file="NavigationServiceImpl.cs" company="CompDJ">
//     Copyright (c) CompDJ. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace T4DemoCs.Navigation
{
    using System;
    using System.Windows.Controls;
    using GalaSoft.MvvmLight.Messaging;

    /// <summary>Navigation Service class implementation</summary>
    internal class NavigationServiceImpl : INavigationService
    {
        /// <summary>Holder for the frame object for navigation.</summary>
        private Frame mainFrame = null;

        /// <summary>Initializes a new instance of the NavigationServiceImpl class.</summary>
        public NavigationServiceImpl()
        {
            Messenger.Default.Register<NavigationFrameMessage>(this, new System.Action<NavigationFrameMessage>(this.NavigationFrameMessageHandler));
        }

        /// <summary>Gets a value indicating whether you can go back</summary>
        public bool CanGoBack
        {
            get
            {
                if (this.mainFrame != null)
                {
                    return this.mainFrame.CanGoBack;
                }

                return false;
            }
        }

        /// <summary>Gets a value indicating whether you can go forward</summary>
        public bool CanGoForward
        {
            get
            {
                if (this.mainFrame != null)
                {
                    return this.mainFrame.CanGoForward;
                }

                return false;
            }
        }

        /// <summary>Go back in the view stack</summary>
        public void GoBack()
        {
            if (this.mainFrame != null)
            {
                if (this.mainFrame.CanGoBack)
                {
                    this.mainFrame.GoBack();
                }
            }
        }

        /// <summary>Go forward in the view stack</summary>
        public void GoForward()
        {
            if (this.mainFrame != null)
            {
                if (this.mainFrame.CanGoForward)
                {
                    this.mainFrame.GoForward();
                }
            }
        }

        /// <summary>Navigate to the specified uri</summary>
        /// <param name="uri">Uri to navigate to</param>
        public void Navigate(string uri)
        {
            if (this.mainFrame != null)
            {
                this.mainFrame.Navigate(new System.Uri(uri, System.UriKind.RelativeOrAbsolute));
            }
        }

        /// <summary>Navigate to the specified uri</summary>
        /// <param name="uri">Uri to navigate to</param>
        public void Navigate(Uri uri)
        {
            if (this.mainFrame != null)
            {
                this.mainFrame.Navigate(uri);
            }
        }

        /// <summary>Navigate to the specified target</summary>
        /// <param name="target">Target to navigate to</param>
        public void Navigate(object target)
        {
            if (this.mainFrame != null)
            {
                this.mainFrame.Navigate(target);
            }
        }

        /// <summary>Handles the navigation and close messages sent from 
        /// view models or commands</summary>
        /// <param name="sender">User interface message</param>
        private void NavigationFrameMessageHandler(NavigationFrameMessage sender)
        {
            this.mainFrame = (Frame)sender.Target;
        }
    }
}
